import os
import re
import numpy as np

#folder_path = './resources/data/2023.12.4'  # 替换为你的文件夹路径
def input_data(file_paths="F:\\pythonProject\\data_processing_platform\\resources\\data\\2023.12.4", laser_point=1000):
    '''
    读入数据
    :param file_paths: 文件路径列表
    :param laser_point: 激光的波峰消失位置，之前的需要裁切
    :return: matrix(np.array)二维矩阵，每一行是对应的光强，一共有n行数据
            percentages(np.array)读入的浓度值 1*n 一共有n行
    '''
    matrix = []
    percentages = []  # 用于存储提取的百分比数据
    if isinstance(file_paths, str):
        # 如果是字符串，加入到列表中
        input_files_list = []
        input_files_list.append(file_paths)
        file_paths = input_files_list
    # 正则表达式模式，用于匹配文件名中的百分比数据
    percentage_pattern = re.compile(r'\d+%')
    for folder_path in file_paths:
        is_folder = os.path.isdir(folder_path)
        if is_folder:
            # 如果是文件夹，则遍历文件夹中的所有文件
            for filename in os.listdir(folder_path):
                if filename.endswith('.txt'):
                    file_path = os.path.join(folder_path, filename)
                    match = percentage_pattern.search(filename)
                    if match:
                        percent = int(match.group()[:-1])
                        percentages.append(percent)
                    with open(file_path, 'r') as file:
                        file_data = []
                        for line in file:
                            line = line.strip()
                            if ',' in line and any(char.isdigit() for char in line):
                                parts = line.split(',')
                                if len(parts) >= 2:
                                    file_data.append(float(parts[1].strip()))
                        matrix.append(file_data)
        else:
            # 如果是单个文件，则直接处理
            match = percentage_pattern.search(os.path.basename(folder_path))
            if match:
                percent = int(match.group()[:-1])
                percentages.append(percent)
            with open(folder_path, 'r') as file:
                file_data = []
                for line in file:
                    line = line.strip()
                    if ',' in line and any(char.isdigit() for char in line):
                        parts = line.split(',')
                        if len(parts) >= 2:
                            file_data.append(float(parts[1].strip()))
                matrix.append(file_data)
    # 将二维列表转换为NumPy二维数组，并切片去除激光部分
    matrix = np.array(matrix)
    matrix = matrix[:, laser_point:]
    # 打印提取的百分比数据列表和NumPy数组的形状
    # print("Extracted percentages:", percentages)
    #print(matrix.shape)
    # 打印NumPy数组
    # print(matrix)
    return matrix, percentages

if __name__ == "__main__":
    # input_data(file_paths=["F:\\pythonProject\\data_processing_platform\\resources\\data\\2023.12.4",
    #                        "F:\\pythonProject\\data_processing_platform\\resources\\data\\2023.12.4\\12.4_830nm_450mw_5000ms(3)_20%c-ATC_3.txt"])
    input_data()